package hust.cs.javacourse.search.query.impl;

import hust.cs.javacourse.search.index.AbstractPosting;
import hust.cs.javacourse.search.index.AbstractTerm;
import hust.cs.javacourse.search.query.AbstractHit;
import hust.cs.javacourse.search.util.FileUtil;

import java.util.Iterator;
import java.util.Map;

public class Hit extends AbstractHit {
    /**
     * 默认构造函数
     */
    public Hit () {
        super();
    }

    /**
     * 构造函数
     * @param docId
     * @param docPath
     */
    public Hit (int docId, String docPath) {
        super(docId, docPath);
    }

    /**
     * 构造函数
     * @param docId
     * @param docPath
     * @param termPostingMapping
     */
    public Hit (int docId, String docPath, Map<AbstractTerm, AbstractPosting> termPostingMapping) {
        super(docId, docPath, termPostingMapping);
    }

    /**
     * 获取文档ID
     * @return
     */
    @Override
    public int getDocId() {
        return this.docId;
    }

    /**
     * 获取文档路径
     * @return
     */
    @Override
    public String getDocPath() {
        return this.docPath;
    }

    /**
     * 获取文档内容
      * @return
     */
    @Override
    public String getContent() {
        return this.content;
    }

    /**
     * 获取文档分数
     * @return
     */
    @Override
    public double getScore() {
        return this.score;
    }

    /**
     * 设置文档内容
     * @param content ：文档内容
     */
    @Override
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 设置文档分数
     * @param score ：文档得分
     */
    @Override
    public void setScore(double score) {
        this.score = score;
    }

    /**
     * 获得命中的单词和对应的Posting键值对
     * @return
     */
    @Override
    public Map<AbstractTerm, AbstractPosting> getTermPostingMapping() {
        return this.termPostingMapping;
    }

    /**
     * 获得命中结果的字符串表示, 用于显示搜索结果
     * @return
     */
    @Override
    public String toString() {
        //高亮显示
        String info = "\033[31;4m" + "docId: " + "\033[0m" + this.docId +
                "\033[31;4m" + "\ndocPath: " + "\033[0m" + this.docPath +
                "\033[31;4m" + "\nscore: " + "\033[0m" + -this.score +
                "\033[31;4m" + "\nposition: " + "\033[0m" + this.getTermPostingMapping().toString() +
                "\033[31;4m" + "\ncontent: \n" + "\033[0m";
//        String info ="docId: " + this.docId +
//                "\ndocPath: " + this.docPath +
//                "\nscore: " + this.score +
//                "\nposition: " + this.getTermPostingMapping().toString() +
//                "\ncontent: \n";
        //this.content = FileUtil.read(this.docPath);

        info = info + this.content + "\n";
        return info;
    }

    /**
     * 比较二个命中结果的大小，根据score比较
     * @param o     ：要比较的名字结果
     * @return
     */
    @Override
    public int compareTo(AbstractHit o) {
        return (int) (this.score - o.getScore());
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Hit) {
            Hit h = (Hit) obj;
            return this.docPath == h.docPath;
        }
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return this.docPath.hashCode();
    }
}
